﻿using Newtonsoft.Json;

namespace Micro.Common.Extensions
{
    public static partial class Extensions
    {
        #region object强制转换dynamic

        /// <summary>
        /// object强制转换dynamic
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static dynamic ToDyc(this object source)
        {
            if (source is null) return default;
            try
            {
                var sourceStr = string.Empty;
                if (source.GetType() == typeof(string))
                    sourceStr = $"{source}";
                else
                    sourceStr = JsonConvert.SerializeObject(source);
                var sourceDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(sourceStr);
                var dic = new System.Dynamic.ExpandoObject() as IDictionary<string, object>;
                foreach (var key in sourceDic) dic.Add(key.Key, key.Value);
                return dic;
            }
            catch
            {
                return default;
            }
        }

        #endregion object强制转换dynamic

        #region List<object>强制转换List<dynamic>

        /// <summary>
        /// 将对象集合转换为dynamic List
        /// </summary>
        public static List<dynamic> ToDycList(this object values)
        {
            var list = new List<dynamic>();
            try
            {
                IEnumerable<object> valueList;
                if (values.GetType() == typeof(string))
                    valueList = JsonConvert.DeserializeObject<List<object>>($"{values}");
                else
                    valueList = values as IEnumerable<object>;
                if (valueList is not null && valueList.Any())
                    list.AddRange(valueList.Select(v => v.ToDyc()));
            }
            catch { }
            return list;
        }

        #endregion List<object>强制转换List<dynamic>

        #region object强制转换dynamic

        /// <summary>
        /// object强制转换dynamic：Newtonsoft
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static dynamic ConvertToDyc(this object source)
        {
            if (source is null) return default;
            try
            {
                if (source.GetType() == typeof(string)) return JsonConvert.DeserializeObject<dynamic>($"{source}");
                return JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(source));
            }
            catch
            {
                return default;
            }
        }

        #endregion object强制转换dynamic

        #region List<object>强制转换List<dynamic>

        /// <summary>
        /// 将对象集合转换为dynamic List：Newtonsoft
        /// </summary>
        public static List<dynamic> ConvertToDycList(this object source)
        {
            if (source is null) return default;
            try
            {
                if (source.GetType() == typeof(string)) return JsonConvert.DeserializeObject<List<dynamic>>($"{source}");
                return JsonConvert.DeserializeObject<List<dynamic>>(JsonConvert.SerializeObject(source));
            }
            catch
            {
                return default;
            }
        }

        #endregion List<object>强制转换List<dynamic>
    }
}